package com.xtravel.tweet.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.xtravel.R;
import com.xtravel.common.helper.LoadingDialog;
import com.xtravel.common.widget.GestureImageLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ImageDetailActivity extends Activity implements OnPageChangeListener {
    private LoadingDialog dialog;
    private ViewPager viewPager;
    private ViewGroup iconGroup;
    private ImageView[] iconViews;
    private List<View> imgViews = new ArrayList<View>();
    private List<String> urlsList = new ArrayList<String>();
    private int startIndex = 0;
    private PagerAdapter imageAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return imgViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View item = imgViews.get(position);
            ((ViewPager) container).addView(item, 0);
            return item;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imgViews.get(position));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tweet_image_detail);
        viewPager = (ViewPager) findViewById(R.id.imagePager);
        iconGroup = (ViewGroup) findViewById(R.id.imgIconGroup);
        Bundle bundle = getIntent().getExtras();
        urlsList = Arrays.asList(bundle.getStringArray("url"));
        startIndex = bundle.getInt("start");
        initImages();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return true;
    }

    Handler loadingHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                dialog.dismiss();
            }
        }
    };

    private void initImages() {
        if (urlsList.size() > 1)
            iconGroup.setVisibility(View.VISIBLE);

        iconViews = new ImageView[urlsList.size()];
        for (int i = 0; i < urlsList.size(); i++) {
            GestureImageLayout il = new GestureImageLayout(this, urlsList.get(i));
            il.findViewById(R.id.ivGesture).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    finish();
                }
            });
            il.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    finish();
                }
            });

            imgViews.add(il);

            ImageView iconImg = new ImageView(this);
            iconViews[i] = iconImg;
            int resId = i == startIndex ? R.drawable.page_indicator_focused : R.drawable.page_indicator_unfocused;
            iconImg.setBackgroundResource(resId);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            iconGroup.addView(iconImg, layoutParams);
        }

        viewPager.setAdapter(imageAdapter);
        viewPager.setCurrentItem(startIndex);
        viewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int index) {
        for (int i = 0; i < iconViews.length; i++) {
            iconViews[i].setBackgroundResource(i == index ? R.drawable.page_indicator_focused : R.drawable.page_indicator_unfocused);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

}
